﻿// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "TestImageGenerator.h"

using namespace HiloTests;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::BulkAccess;

concurrency::task<FileInformation^> TestImageGenerator::CreateTestImageFileFromLocalFolder( 
    Platform::String^ fileName, 
    Platform::String^ newName)
{
    StorageFolder^ installStorageFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
    return concurrency::create_task(installStorageFolder->GetFileAsync("Images\\" + fileName))
        .then([newName](Windows::Storage::StorageFile^ file) {
			concurrency::task_status status;
			auto result = TestHelper::RunSynced(concurrency::create_task(
				file->CopyAsync(Windows::Storage::KnownFolders::PicturesLibrary, newName, Windows::Storage::NameCollisionOption::ReplaceExisting)
				), status);
			return concurrency::task_from_result(result);
    }).then([](Windows::Storage::StorageFile^ file) {
        auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
        fileTypeFilter->Append(".png");
        fileTypeFilter->Append(".jpg");
        auto picturesFolder = Windows::Storage::KnownFolders::PicturesLibrary;
        auto queryOptions = ref new Windows::Storage::Search::QueryOptions(Windows::Storage::Search::CommonFileQuery::OrderByDate, fileTypeFilter);
        queryOptions->FolderDepth = Windows::Storage::Search::FolderDepth::Deep;
        queryOptions->IndexerOption = Windows::Storage::Search::IndexerOption::DoNotUseIndexer;
        queryOptions->ApplicationSearchFilter = "System.FileName:=\"" + file->Name + "\"";
        auto fileQuery = picturesFolder->CreateFileQueryWithOptions(queryOptions);
        auto fileInformationFactory = ref new Windows::Storage::BulkAccess::FileInformationFactory(
            fileQuery, 
            Windows::Storage::FileProperties::ThumbnailMode::PicturesView);
		concurrency::task_status status;
		auto result = TestHelper::RunSynced(concurrency::create_task(
			fileInformationFactory->GetFilesAsync()
			), status);
		return concurrency::task_from_result(result);
    }).then([this](Windows::Foundation::Collections::IVectorView<Windows::Storage::BulkAccess::FileInformation^>^ files) {
        auto file = files->GetAt(0);
        m_createdFiles.push_back(file);
        return file;
    });
}

concurrency::task<void> HiloTests::TestImageGenerator::DeleteFilesAsync()
{
    for (auto filePath : m_createdFiles)
    {
		concurrency::task_status status;
		TestHelper::RunSynced(concurrency::create_task(filePath->DeleteAsync()), status);
    }

	return concurrency::task_from_result();
}

concurrency::task<IVectorView<Windows::Storage::StorageFile^>^> HiloTests::TestImageGenerator::CreateTestImagesFromLocalFolder(Platform::String^ fileName, unsigned int count, Platform::String^ nameBase /*= "test"*/, Platform::String^ extension /*= ".png" */)
{
    Windows::Storage::StorageFolder^ installStorageFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
    return concurrency::create_task(installStorageFolder->GetFileAsync("Images\\" + fileName))
        .then([nameBase, extension, count](Windows::Storage::StorageFile^ file) {

			std::vector<Windows::Storage::StorageFile^> result;

            for(unsigned int i=0; i < count; i++)
            {
                Platform::String^ newName = nameBase + i + extension;

				concurrency::task_status status;
				result.push_back(TestHelper::RunSynced(concurrency::create_task(
					file->CopyAsync(Windows::Storage::KnownFolders::PicturesLibrary, newName, Windows::Storage::NameCollisionOption::ReplaceExisting))
					, status));
            }
			return concurrency::task_from_result(result);
    }).then([extension, count](std::vector<Windows::Storage::StorageFile^> files) {
        auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
        fileTypeFilter->Append(extension);
        auto picturesFolder = Windows::Storage::KnownFolders::PicturesLibrary;
        auto queryOptions = ref new Windows::Storage::Search::QueryOptions(Windows::Storage::Search::CommonFileQuery::OrderByDate, fileTypeFilter);
        queryOptions->FolderDepth = Windows::Storage::Search::FolderDepth::Deep;
        queryOptions->IndexerOption = Windows::Storage::Search::IndexerOption::DoNotUseIndexer;
        Platform::String^ filter = "";

        for (auto file : files)
        {             
            if (filter != "")
            {
                filter += " OR ";
            }

            filter += "System.FileName:=\"" + file->Name + "\"";
        }

        queryOptions->ApplicationSearchFilter = filter;
        auto fileQuery = picturesFolder->CreateFileQueryWithOptions(queryOptions);
		auto result = ref new Platform::Collections::Vector<StorageFile^>();
		for (decltype(count) i = 0; i < count; ++i)
		{
			concurrency::task_status status;
			auto v = TestHelper::RunSynced(concurrency::create_task(fileQuery->GetFilesAsync(i, 1)), status);
			result->Append(v->GetAt(0));
		}
		return concurrency::task_from_result(result->GetView());
    }).then([this](IVectorView<StorageFile^>^ files)
    {
        for (auto item : files)
        {
            m_createdFiles.push_back(item);
        }

        return files;
    });

}
